package zhang.lao.console.controller.wn;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zhang.lao.annotation.RepeatSubmit;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachineDevice;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachineDeviceActive;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachineDeviceActiveGt06;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachineDeviceActiveGt06Example;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.build.tool.date.DateStyle;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.dao.base.WnMachineDeviceActiveGt06Dao;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.map.ActiveDevice;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.req.WnMacheineDeviceImeiReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.service.console.base.WnMachineDeviceImeiService;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
* 基于电话卡的设备
* @author ZhangQin
* @create 2017年08月28日 17:02
**/
@Controller
public class WnMachineDeviceImeiController{
	@Resource
	private WnMachineDeviceImeiService wnMachineDeviceImeiService;
	@Resource
	private WnMachineDeviceActiveGt06Dao wnMachineDeviceActiveGt06Dao;
	@RequestMapping("/console/wn_machine_device_imei/add")
	@RepeatSubmit(isAdd = true)
	public String add(){
		return wnMachineDeviceImeiService.add();
	}

	@RequestMapping("/console/wn_machine_device_imei/addandlink/{machineId}")
	@RepeatSubmit(isAdd = true)
	public String addBymachineId(@PathVariable String machineId, ModelMap modelMap){
		modelMap.put(machineId,"machineId");
		return wnMachineDeviceImeiService.add();
	}

	@RequestMapping("/console/wn_machine_device_imei/active/number")
	@RepeatSubmit(isAdd = true)
	public @ResponseBody HttpResult<ActiveDevice> activeDevice(String farmIds,ConsoleContext consoleContext){
		ActiveDevice activeDevice = new ActiveDevice();


		WnMachineDeviceActiveGt06Example wnMachineDeviceActiveGt06Example = new WnMachineDeviceActiveGt06Example();
		WnMachineDeviceActiveGt06Example.Criteria criteria = wnMachineDeviceActiveGt06Example.createCriteria();
		if(StringUtils.isNotBlank(farmIds)){
			criteria.andCreateDateEqualTo(DateUtil.StringToDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD))).andFarmIdIn(Lists.newArrayList(farmIds.split(",")));
		}else{
			criteria.andCreateDateEqualTo(DateUtil.StringToDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD))).andFarmIdIn(consoleContext.getFarms());
		}
		activeDevice.setActiveNumber(wnMachineDeviceActiveGt06Dao.countByExample(wnMachineDeviceActiveGt06Example));
		Calendar cal = Calendar. getInstance ();
		cal.add(Calendar.MINUTE, -5 ) ;
		criteria.andCreateTimeGreaterThan(cal.getTime());
		activeDevice.setNowNumber(wnMachineDeviceActiveGt06Dao.countByExample(wnMachineDeviceActiveGt06Example));
		return HttpResultUtil.buildSuccess(activeDevice);
	}

	@RepeatSubmit(isAdd = true)
	@RequestMapping("/console/wn_machine_device_imei/edit")
	public String edit(ModelMap modelMap,Integer id){
		return  wnMachineDeviceImeiService.edit(modelMap,id);
	}

	@RequestMapping("/console/wn_machine_device_imei/list")
	public String list(){
		return  wnMachineDeviceImeiService.list();
	}

	@RequestMapping("/console/wn_machine_device_imei/json")
	public @ResponseBody BootStrapGridResp json(BootStrapGridReq bootGridReq,WnMacheineDeviceImeiReq wnMacheineDeviceImeiReq,ConsoleContext consoleContext){
		return wnMachineDeviceImeiService.json(bootGridReq,wnMacheineDeviceImeiReq,consoleContext);
	}

	@RepeatSubmit(isAdd = false)
	@RequestMapping("/console/wn_machine_device_imei/save")
	public @ResponseBody HttpResult save(String formObjectJson,String machineId, ConsoleContext consoleContext){
		return wnMachineDeviceImeiService.save(formObjectJson,machineId,consoleContext);

	}

	@RequestMapping("/console/wn_machine_device_imei/delete")
	public @ResponseBody HttpResult delete(String ids){
        return wnMachineDeviceImeiService.delete(ids);
	}

	public static void main(String[] args) {
		Calendar cal = Calendar. getInstance ();
		cal.add(Calendar.MINUTE, -1 ) ;
		System.out.println(cal.getTime());
	}
}
